package component
{
	import _me.Scene_data;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	
	import mx.core.UIComponent;
	
	public class Timeline extends UIComponent
	{
		private var bitmap:Bitmap = new Bitmap;;
		private var playhead:Shape;
		private var baseW:int = 8;//#D6D6D6
		private static var instance:Timeline;
		private var infoTxt:TextField;
		private var allFrameNum:int;
		public function Timeline()
		{
			super();
			//init();
			this.addChild(bitmap);
			bitmap.y = 10;
			//setFrameNum(200);
			initPlayHead();
			initTxt();
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		public static function getInstance():Timeline{
			if(instance == null){
				instance = new Timeline;
			}
			return instance;
		}
		public function setFrameNum(num:int):void{
			var bitmapdata:BitmapData = new BitmapData(8*num,40,false,0xC0E9E9);
			var sp1:Shape = new Shape();
			sp1.graphics.beginFill(0xffffff,1);
			sp1.graphics.lineStyle(1,0xD6D6D6);
			sp1.graphics.drawRect(0,0,baseW,20);
			sp1.graphics.endFill();
			
			var sp2:Shape = new Shape();
			sp2.graphics.beginFill(0xEBEBEB,1);
			sp2.graphics.lineStyle(1,0xD6D6D6);
			sp2.graphics.drawRect(0,0,baseW,20);
			sp2.graphics.endFill();
			
			var txt:TextField = new TextField();
			txt.width = 25;
			txt.height = 20;
			var ma:Matrix = new Matrix();
			ma.ty = 10;
			var txtma:Matrix = new Matrix();
			txtma.ty = -5;
			for(var i:int;i<num;i++){
				if(i%5){
					bitmapdata.draw(sp1,ma);
				}else{
					bitmapdata.draw(sp2,ma);
					txt.htmlText = "<font size='10' face='微软雅黑'><b>" + i + "</b></font>"
					bitmapdata.draw(txt,txtma);
				}
				ma.tx = i*baseW;
				txtma.tx = i*baseW;
			}
			bitmap.bitmapData = bitmapdata;
			this.width = bitmap.width;
			allFrameNum = num;
		}
		private function initPlayHead():void{
			playhead = new Shape();
			playhead.graphics.lineStyle(1,0xCC0000);
			playhead.graphics.beginFill(0xCC0000,0.5);
			playhead.graphics.drawRect(0,0,baseW,20);
			playhead.graphics.endFill();
			playhead.graphics.lineStyle(1,0xCC0000);
			playhead.graphics.moveTo(baseW/2,20);
			playhead.graphics.lineTo(baseW/2,50);
			this.addChild(playhead);
		}
		private function initTxt():void{
			infoTxt = new TextField;
			infoTxt.width = 80;
			infoTxt.height = 20;
			this.addChild(infoTxt);
			infoTxt.x = -80;
			infoTxt.y = 52;
		}
		private function onClick(event:MouseEvent):void{
			var xpos:int = this.mouseX;
			Scene_data.roleLocal.pause = true;
			Scene_data.roleLocal.curentFrame = int(xpos/baseW);
			
		}
		public function setFrame(frame:int):void{
			playhead.x = frame*baseW;
			infoTxt.htmlText = "<p align='right'><font color='#ff0000'>" + frame +　"</font>/" + allFrameNum + "</p>";
		}
		
	}
}